package com.chalk.config.base;

import com.baomidou.mybatisplus.plugins.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;


/**
 * @author xiaoqiang
 * @Description controller基类
 * @date 2019/3/21 18:01
 */
public abstract class BaseController<T> {
    @Autowired
    private HttpServletRequest request;

    public HttpServletRequest getRequest(){
        return request;
    }
    protected final Logger logger = LoggerFactory.getLogger(getClass().getName());

    protected void beanValidator(BindingResult result) {
        if (result.hasErrors()) {
            StringBuilder sb = new StringBuilder();
            for (ObjectError error : result.getAllErrors()) {
                sb.append(error.getDefaultMessage());
                sb.append("\r\n");
            }

            throw new ValidationException(sb.toString());
        }
    }

    public Page<T> getBasePage(Integer current, Integer size) {
        Page<T> page = new Page<>();
        if (current != null) {
            page.setCurrent(current);
        }
        if (size != null) {
            page.setSize(size);
        }
        return page;
    }

}
